package vn.pyco.prez.tapestry.pages;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import vn.pyco.prez.PrezConstants;
import vn.pyco.prez.tapestry.services.AppHelper;

@Import(
	stack=PrezConstants.COMETD_JSSTACK,
	library={
			"${prez.scripts}/jquery.cometd.settings.js",
			"${prez.scripts}/jquery.cometd.prezremote.js"
	}
)
public class Remote {
	
	@Inject
	private JavaScriptSupport _jsSupport;	
	
	@Inject
    private AppHelper _appHelper;
	
	@Inject
	@Symbol(SymbolConstants.PRODUCTION_MODE)
	private boolean _productionMode;
	
	private String _taskId;
	
	public void onActivate(String taskId) {
		_taskId=taskId;
	}
	
	@AfterRender
	public void afterRender(){
		_jsSupport.addScript("CometSettings.init('%s','%s','%s','%s');",
				_appHelper.getFQNRootUrl(Boolean.FALSE) + PrezConstants.COMETD_URL,
				_productionMode ? "warn" : "info",
				_taskId,
				"null"
				);
		_jsSupport.addScript("CometPrezRemote.init();");
	}	
}
